<?php

class register_tutor extends Controller {

	function Welcome()
	{
		parent::Controller();	
	}
	
	function index()
	{
		$data['form_status'] = 'add';
		$this->load->model('filemanagementmodel');
	    $data['header_tel'] = $this->filemanagementmodel->getByPostion('header_tel');
	    $data['footer_address'] = $this->filemanagementmodel->getByPostion('footer_add1');
	    $data['apply_tutor'] = $this->filemanagementmodel->getByPostion('apply_tutor');
	    
		$this->load->view('f_header', $data);
		$this->load->view('f_register_tutor', $data);
		$this->load->view('f_footer', $data);
	}
	
	function edit()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }

	    $this->load->model('filemanagementmodel');
	    $data['apply_tutor'] = $this->filemanagementmodel->getByPostion('apply_tutor');
	    
		$this->load->view('header');
		$this->load->view('tutor-edit-contact-form', $data);
		$this->load->view('footer');
	}
	
	function save()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    
	    $this->load->model('filemanagementmodel');
	    $data['position'] = 'apply_tutor';
	    $data['file_name'] = $this->input->post('txtDetail');
		$this->filemanagementmodel->update($data);
		
		$this->msg = "บันทึกข้อมูลเรียบร้อย";
		
		$this->load->model('filemanagementmodel');
	    $data['apply_tutor'] = $this->filemanagementmodel->getByPostion('apply_tutor');
		$this->load->view('header');
		$this->load->view('tutor-edit-contact-form', $data);
		$this->load->view('footer', $this->msg);
	}
	
	function saveApplication() 
	{
		$this->load->model('TutorModel');
		$this->load->model('TutorSchoolModel');
		$data = array();
		$title = $this->input->post('radTitle');
		$titleOther = $this->input->post('txtTitleOther');
		if($title != 'อื่นๆ') {
			$data['title'] = $title;
		} else {
			$data['title'] = $titleOther;
		}
		$data['first_name_th'] = $this->input->post('txtFirstNameTh');
		$data['last_name_th'] = $this->input->post('txtLastNameTh');
		$data['nick_name'] = $this->input->post('txtNickName');
		$data['first_name_en'] = $this->input->post('txtFirstNameEn');
		$data['last_name_en'] = $this->input->post('txtLastNameEn');
		$data['citizen_id'] = $this->input->post('txtCitizenId') ? $this->input->post('txtCitizenId') : null;
		$data['comment'] = $this->input->post('txtComment') ? $this->input->post('txtComment') : null;
		$data['email'] = $this->input->post('txtEmail') ? $this->input->post('txtEmail') : null;    
	    $data['telephone_home'] = $this->input->post('txtTel') ? $this->input->post('txtTel') : null;
	    $data['mobile01'] = $this->input->post('txtMobile01') ? $this->input->post('txtMobile01') : null;
	    
		$netMobile = $this->input->post('selNetMobile01') ? $this->input->post('selNetMobile01') : null;
		$netMobileOther = $this->input->post('txtNetMobile01') ? $this->input->post('txtNetMobile01') : null;
		if($netMobile != 'อื่นๆ (โปรดระบุ)') {
			$data['mobile01_network'] = $netMobile;
		} else {
			$data['mobile01_network'] = $netMobileOther;
		}
	    
		$data['mobile02'] = $this->input->post('txtMobile02') ? $this->input->post('txtMobile02') : null;
		$netMobile = $this->input->post('selNetMobile02') ? $this->input->post('selNetMobile02') : null;
		$netMobileOther = $this->input->post('txtNetMobile02') ? $this->input->post('txtNetMobile02') : null;
		if($netMobile != 'อื่นๆ (โปรดระบุ)') {
			$data['mobile02_network'] = $netMobile;
		} else {
			$data['mobile02_network'] = $netMobileOther;
		}
		
	    $data['add_room_no'] = $this->input->post('txtRoomNo') ? $this->input->post('txtRoomNo') : null;
	    $data['add_home_no'] = $this->input->post('txtHomeNo') ? $this->input->post('txtHomeNo') : null;
	    $data['add_building_name'] = $this->input->post('txtBuilding') ? $this->input->post('txtBuilding') : null;
	    $data['add_moo'] = $this->input->post('txtMoo') ? $this->input->post('txtMoo') : null;
	    $data['add_soi'] = $this->input->post('txtSoi') ? $this->input->post('txtSoi') : null;
	    $data['add_road'] = $this->input->post('txtRoad') ? $this->input->post('txtRoad') : null;
	    $data['add_distinct'] = $this->input->post('txtDistinct') ? $this->input->post('txtDistinct') : null;
	    $data['add_area'] = $this->input->post('txtArea') ? $this->input->post('txtArea') : null;
	    $data['add_province'] = $this->input->post('txtProvince') ? $this->input->post('txtProvince') : null;
	    $data['add_post_code'] = $this->input->post('txtPostCode') ? $this->input->post('txtPostCode') : null;
	    $data['add_note'] = $this->input->post('txtAddNote') ? $this->input->post('txtAddNote') : null;
	    $data['source'] = 'C';
	    
		$id = $this->TutorModel->addFromC($data, $this->input->post('txtBirthday'));
		$scArray = array('tutor_id' => $id, 'shool_id' => '1');
		$this->TutorSchoolModel->add($scArray);
		$scArray = array('tutor_id' => $id, 'shool_id' => '11');
		$this->TutorSchoolModel->add($scArray);
		
		$d['id'] = $id;
		if(isset($_FILES) && @$_FILES['txtTutorImg']['error'] == '0') {
			$config['upload_path'] = './data-image/tutor/';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '2048';
			$config['overwrite'] = TRUE;
			$config['file_name'] = $id.'-profile';
			$this->load->library('upload', $config);
			if ( ! $this->upload->do_upload('txtTutorImg'))
			{
				$this->msg = "ไม่สามารถ upload รูปภาพได้";			
			}
			else
			{
				$data = array('upload_data' => $this->upload->data());
				$Imgdata = array(
				   'tutor_id' => $id, 
	               'tutor_image' => $data['upload_data']['file_name']   
	            );
				$this->TutorModel->updateTutorImagePath($Imgdata);
				$this->msg = "บันทึกข้อมูลเรียบร้อย";
			}
		}
		$data = array();
		$this->load->model('filemanagementmodel');
	    $data['header_tel'] = $this->filemanagementmodel->getByPostion('header_tel');
	    $data['footer_address'] = $this->filemanagementmodel->getByPostion('footer_add1');
	    $d['apply_tutor'] = $this->filemanagementmodel->getByPostion('apply_tutor');
	    
		$this->load->view('f_header', $data);
		$this->load->view('f_register_tutor_2', $d);
		$this->load->view('f_footer', $data);
	}
	
	function updateApplication() {
	$id = $this->input->post('txtID');
		if($id != null) {
			$this->load->model('TutorModel');
			$data['tutor_id'] = $id; 
			$data['edu_status'] = $this->input->post('rdEduStatus') ? $this->input->post('rdEduStatus') : null;
			$data['edu_current_level'] = $this->input->post('studyingLevel') ? $this->input->post('studyingLevel') : null;
			$data['edu_current_university'] = $this->input->post('studyingUniversity') ? $this->input->post('studyingUniversity') : null;
			$data['edu_current_department'] = $this->input->post('studyingMajor') ? $this->input->post('studyingMajor') : null;
			$data['edu_current_student_id'] = $this->input->post('studyingID') ? $this->input->post('studyingID') : null;
			$data['edu_current_class'] = $this->input->post('studyingClass') ? $this->input->post('studyingClass') : null;
			$data['edu_current_GPA'] = $this->input->post('studyingGPA') ? $this->input->post('studyingGPA') : null;
			$data['edu_pratom_school'] = $this->input->post('pratomSchool') ? $this->input->post('pratomSchool') : null;
			$data['edu_pratom_major'] = $this->input->post('pratomMajor') ? $this->input->post('pratomMajor') : null;
			$data['edu_pratom_student_id'] = $this->input->post('pratomID') ? $this->input->post('pratomID') : null;
			$data['edu_pratom_GPA'] = $this->input->post('pratomGPA') ? $this->input->post('pratomGPA') : null;
			$data['edu_mo_tom_school'] = $this->input->post('tonSchool') ? $this->input->post('tonSchool') : null;
			$data['edu_mo_tom_major'] = $this->input->post('tonMajor') ? $this->input->post('tonMajor') : null;
			$data['edu_mo_tom_student_id'] = $this->input->post('tonID') ? $this->input->post('tonID') : null;
			$data['edu_mo_tom_GPA'] = $this->input->post('tonGPA') ? $this->input->post('tonGPA') : null;
			$data['edu_mo_pay_school'] = $this->input->post('paySchool') ? $this->input->post('paySchool') : null;
			$data['edu_mo_pay_major'] = $this->input->post('payMajor') ? $this->input->post('payMajor') : null;
			$data['edu_mo_pay_student_id'] = $this->input->post('payID') ? $this->input->post('payID') : null;
			$data['edu_mo_pay_GPA'] = $this->input->post('payGPA') ? $this->input->post('payGPA') : null;
			$data['edu_bachelor_university'] = $this->input->post('bSchool') ? $this->input->post('bSchool') : null;
			$data['edu_bachelor_major'] = $this->input->post('bMajor') ? $this->input->post('bMajor') : null;
			$data['edu_bachelor_student_id'] = $this->input->post('bID') ? $this->input->post('bID') : null;
			$data['edu_bachelor_GPA'] = $this->input->post('bGPA') ? $this->input->post('bGPA') : null;
			
			$data['edu_master_university'] = $this->input->post('mSchool') ? $this->input->post('mSchool') : null;
			$data['edu_master_major'] = $this->input->post('mMajor') ? $this->input->post('mMajor') : null;
			$data['edu_master_student_id'] = $this->input->post('mID') ? $this->input->post('mID') : null;
			$data['edu_master_GPA'] = $this->input->post('mGPA') ? $this->input->post('mGPA') : null;
			
			$data['edu_doctor_university'] = $this->input->post('dSchool') ? $this->input->post('dSchool') : null;
			$data['edu_doctor_major'] = $this->input->post('dMajor') ? $this->input->post('dMajor') : null;
			$data['edu_doctor_student_id'] = $this->input->post('dID') ? $this->input->post('dID') : null;
			$data['edu_doctor_GPA'] = $this->input->post('dGPA') ? $this->input->post('dGPA') : null;
			
			$data['teach01_subject'] = $this->input->post('txtTeach01Subject') ? $this->input->post('txtTeach01Subject') : null;
			$data['teach01_class'] = $this->input->post('txtTeach01') ? $this->input->post('txtTeach01') : null;
			$data['teach01_inter'] = $this->input->post('txtTeach01Inter') ? $this->input->post('txtTeach01Inter') : null;
			$data['teach02_subject'] = $this->input->post('txtTeach02Subject') ? $this->input->post('txtTeach02Subject') : null;
			$data['teach02_class'] = $this->input->post('txtTeach02') ? $this->input->post('txtTeach02') : null;
			$data['teach02_inter'] = $this->input->post('txtTeach02Inter') ? $this->input->post('txtTeach02Inter') : null;
			$data['teach03_subject'] = $this->input->post('txtTeach03Subject') ? $this->input->post('txtTeach03Subject') : null;
			$data['teach03_class'] = $this->input->post('txtTeach03') ? $this->input->post('txtTeach03') : null;
			$data['teach03_inter'] = $this->input->post('txtTeach03Inter') ? $this->input->post('txtTeach03Inter') : null;
			$data['teach04_subject'] = $this->input->post('txtTeach04Subject') ? $this->input->post('txtTeach04Subject') : null;
			$data['teach04_class'] = $this->input->post('txtTeach04') ? $this->input->post('txtTeach04') : null;
			$data['teach04_inter'] = $this->input->post('txtTeach04Inter') ? $this->input->post('txtTeach04Inter') : null;
			$data['teach05_subject'] = $this->input->post('txtTeach05Subject') ? $this->input->post('txtTeach05Subject') : null;
			$data['teach05_class'] = $this->input->post('txtTeach05') ? $this->input->post('txtTeach05') : null;
			$data['teach05_inter'] = $this->input->post('txtTeach05Inter') ? $this->input->post('txtTeach05Inter') : null;
			$data['teach06_subject'] = $this->input->post('txtTeach06Subject') ? $this->input->post('txtTeach06Subject') : null;
			$data['teach06_class'] = $this->input->post('txtTeach06') ? $this->input->post('txtTeach06') : null;
			$data['teach06_inter'] = $this->input->post('txtTeach06Inter') ? $this->input->post('txtTeach06Inter') : null;
			
			$data['teach_area'] = $this->input->post('txtArea') ? $this->input->post('txtArea') : null;
			$data['hist_profile'] = $this->input->post('txtExp') ? $this->input->post('txtExp') : null;
			$data['hist_related_work'] = $this->input->post('txtExpMajor') ? $this->input->post('txtExpMajor') : null;
			$data['hist_note'] = $this->input->post('txtNote') ? $this->input->post('txtNote') : null;
			$sub = $this->input->post('selKnown') ? $this->input->post('selKnown') : null;
			$subOter = $this->input->post('txtKnownOther') ? $this->input->post('txtKnownOther') : null;
			if($sub != 'มีผู้แนะนำ') {
				$data['known_from'] = $sub;
			} else {
				$data['known_from'] = $subOter;
			}
			
			$otherArr = $this->input->post('subOth');
			$data['teach_other_1'] = $otherArr[0] ? $otherArr[0] : null;
			$data['teach_other_2'] = $otherArr[1] ? $otherArr[1] : null;
			$data['teach_other_3'] = $otherArr[2] ? $otherArr[2] : null;
			$data['teach_other_4'] = $otherArr[3] ? $otherArr[3] : null;
			$data['teach_other_5'] = $otherArr[4] ? $otherArr[4] : null;
			$data['teach_other_6'] = $otherArr[5] ? $otherArr[5] : null;
			
			$this->TutorModel->updateFromC($data);
			$data = array();
			$data['tutor_id'] = $id; 
			if(isset($_FILES) && @$_FILES['txtResume']['error'] == '0') {
				$config['upload_path'] = './data-image/tutor/';
				$config['allowed_types'] = 'gif|jpg|png|pdf|zip|doc|tif';
				$config['max_size']	= '5048';
				$config['overwrite'] = TRUE;
				$config['file_name'] = $id.'-resume';
				$this->load->library('upload', $config);
				if ( ! $this->upload->do_upload('txtResume'))
				{
					//$this->msg = "ไม่สามารถ upload ไฟล์ได้";			
				}
				else
				{
					$dataUpload = array('upload_data' => $this->upload->data());
					$data['hist_resume_path'] = $dataUpload['upload_data']['file_name'];
					//$this->msg = "บันทึกข้อมูลเรียบร้อย";
					$this->TutorModel->updateTutorResumePath($data);
				}
			}
			
				// send mail;
		$ttt = $this->TutorModel->getByID($id);
		$this->load->library('email');
		$config['charset'] = 'utf-8';
		$config['wordwrap'] = TRUE;
		$config['mailtype'] = 'html';
		$this->email->initialize($config);		
		$this->email->from('web_admin@tutor-land.com');
		$this->email->to('hr_bvk@hotmail.com');
		$this->email->bcc('pun.spider@gmail.com');
		$this->email->subject('[C] มีติวเตอร์สมัครมาใหม่  คุณ ' . $ttt['first_name_th']);
		
		$msgMail = '
		<html>
		<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		</head>
		
		<body>
		<p>มีติวเตอร์สมัครมาใหม่  คุณ' . $ttt['first_name_th'] . ' ' .$ttt['last_name_th']. '</p>
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
		  <tr>
		    <td>โทรศัพท์</td>
		    <td>'. $ttt['mobile01'] .'</td>
		  </tr>
		  <tr>
		    <td>วิชาที่สอนได้</td>
		    <td>'. $ttt['teach01_subject'] . ' ' . $ttt['teach02_subject']. ' ' . $ttt['teach03_subject']. ' ' . $ttt['teach04_subject']. ' ' . $ttt['teach05_subject']. ' ' . $ttt['teach06_subject'] .'</td>
		  </tr>
		</table>
		<br />
		<hr />
		<div align="right"><em>ข้อความนี้ถูกส่งโดยระบบหลังบ้านของติวเตอร์แลนด์<br /> 
		' . date('jS F Y h:i:s A') .'</em></div>
		</body></html>';
		$this->email->message($msgMail);
		$this->email->send();
			
			$msg['success'] = "ได้รับข้อมูลเรียบร้อยแล้วค่ะ ทางโรงเรียนจะติดต่อกลับไปเร็วที่สุดค่ะ";
			$msg['form_status'] = 'add';
			
			$data = array();
			$this->load->model('filemanagementmodel');
		    $data['header_tel'] = $this->filemanagementmodel->getByPostion('header_tel');
		    $data['footer_address'] = $this->filemanagementmodel->getByPostion('footer_add1');
		    $msg['apply_tutor'] = $this->filemanagementmodel->getByPostion('apply_tutor');
		    
			$this->load->view('f_header', $data);
			$this->load->view('f_register_tutor', $msg);
			$this->load->view('f_footer', $data);
		} else {
			$this->index();
		}
	}
}

?>